package org.gnyxy.controller.front;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.gnyxy.context.BaseContext;
import org.gnyxy.dto.CartDTO;
import org.gnyxy.dto.CartInfoDTO;
import org.gnyxy.dto.CartNumberDTO;
import org.gnyxy.result.Result;
import org.gnyxy.service.CartService;
import org.gnyxy.vo.CartInfoVOWithAmount;
import org.gnyxy.vo.CartVO;
import org.gnyxy.vo.CartVOWithAmount;
import org.gnyxy.vo.CartWithInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/front/api/cart")
@Slf4j
@Api(tags = "小程序端-购物车")
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 添加购物车
     * @param cartDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result add(@RequestBody CartDTO cartDTO){
        log.info("添加购物车: {}",cartDTO);
        cartService.add(cartDTO);
        return Result.success();
    }

    /**
     * 查看当前用户的购物车以及详情
     * @return
     */
    @GetMapping("/getCart")
    @ApiOperation("查看当前用户的购物车")
    public Result<CartVOWithAmount> getCart(){
        log.info("查看当前用户的购物车");
        // 从当前线程中获取userId
        Long userId = BaseContext.getCurrentId();
        CartVOWithAmount cartVOWithAmount = cartService.getByUserId(userId);
        return Result.success(cartVOWithAmount);
    }

    @GetMapping("/getCartInfos/{cartId}")
    @ApiOperation("获取当前购物车的菜品的购物车详情")
    public Result getCartInfos(@PathVariable Long cartId){
        CartInfoVOWithAmount cartInfoVOWithAmount = cartService.getCartInfos(cartId);
        return Result.success(cartInfoVOWithAmount);
    }


    /**
     * 修改购物车里的菜品材料数量
     * @param cartInfoDTO
     * @return
     */
    @PostMapping("/updateMaterialNumber")
    @ApiOperation("修改购物车里的菜品的菜品材料数量")
    public Result updateMaterialNumber(@RequestBody CartInfoDTO cartInfoDTO){
        log.info("修改购物车里的菜品材料数量: {}",cartInfoDTO);
        cartService.updateMaterialNumber(cartInfoDTO);
        return Result.success();
    }

    /**
     * 更新菜品数量
     * @param cartNumberDTO
     * @return
     */
    @PostMapping("/updateCartNumber")
    @ApiOperation("修改购物车里的菜品数量")
    public Result updateCartNumber(@RequestBody CartNumberDTO cartNumberDTO){
        log.info("修改购物车里的菜品数量: {}",cartNumberDTO);
        cartService.updateCartNumber(cartNumberDTO);
        return Result.success();
    }

    /**
     * 删除购物车及详情
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    @ApiOperation("删除购物车及详情")
    public Result deleteCart(@PathVariable Long id){
        log.info("删除购物车及详情: {}",id);
        cartService.deleteCart(id);
        return Result.success();
    }

}
